package com.yidu.drug.service.impl;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.drug.dao.QualityTestingModelMapper;
import com.yidu.drug.model.QualityTestingModel;
import com.yidu.drug.service.QualityTestingModelService;
@Service
public class QualityTestingModelServicelmpl implements QualityTestingModelService{

	@Resource
	private QualityTestingModelMapper qualityTestingModelDao;

	@Override
	public int deleteByPrimaryKey(String qtId) {
		// TODO Auto-generated method stub
		return qualityTestingModelDao.deleteByPrimaryKey(qtId);
	}

	@Override
	public int insert(QualityTestingModel record) {
		// TODO Auto-generated method stub
		return qualityTestingModelDao.insert(record);
	}

	@Override
	public int insertSelective(QualityTestingModel qualityTestingModel) {
		System.out.println("进了增加的方法-=-=-=-=-=-=-=-=-=-=-=-=");


		String qtId = UUID.randomUUID().toString().replaceAll("-", "");

		Timestamp t = new Timestamp(new Date().getTime());

		qualityTestingModel.setQtTime(t);
		//qualityTestingModel.setQtId(qtId);
		return qualityTestingModelDao.insertSelective(qualityTestingModel);

		//qualityTestingModelService.insertSelective(qualityTestingModel);

	}

	@Override
	public QualityTestingModel selectByPrimaryKey(String qtId) {
		// TODO Auto-generated method stub
		return qualityTestingModelDao.selectByPrimaryKey(qtId);
	}

	@Override
	public int updateByPrimaryKeySelective(QualityTestingModel record) {
		// TODO Auto-generated method stub
		return qualityTestingModelDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(QualityTestingModel record) {
		// TODO Auto-generated method stub
		return qualityTestingModelDao.updateByPrimaryKey(record);
	}

	@Override
	public List<QualityTestingModel> findByfdPwId(String pwId) {
		// TODO Auto-generated method stub
		return qualityTestingModelDao.findByfdPwId(pwId);
	}









	@Override
	public Long findRows(QualityTestingModel record) {
		Long count = qualityTestingModelDao.findRows(record);
		return count;
	}

	@Override
	public List<QualityTestingModel> findAllQualityTestingModel(QualityTestingModel record) {
		List<QualityTestingModel> list = qualityTestingModelDao.findAllQualityTestingModel(record);
		return list;
	}

	@Override
	public QualityTestingModel findById(String picId) {
		QualityTestingModel record = qualityTestingModelDao.selectByPrimaryKey(picId);
		return record;
	}

	/**
	 * 计算
	 */
	@Override
	public String update(QualityTestingModel qualityTestingModel) {

		qualityTestingModel.getNopassNum();
		qualityTestingModel.getQtSumnum();
		System.out.println("未通过质检数："+qualityTestingModel.getNopassNum());
		System.out.println("总质检数："+qualityTestingModel.getQtSumnum());


		System.out.println("未通过数量:"+qualityTestingModel.getNopassNum()+" 总数量:"+qualityTestingModel.getQtSumnum()
		+"通过的数量:"+(qualityTestingModel.getQtSumnum()-qualityTestingModel.getNopassNum()));
		
		//int rows = qualityTestingModelDao.updateByPrimaryKeySelective(qualityTestingModel);
		//算qtRate的值=总-未/总 ：需要转double型 小数不能被除
		double qtRate = ((double)qualityTestingModel.getQtSumnum()-(double)qualityTestingModel.getNopassNum())/qualityTestingModel.getQtSumnum();
		System.out.println("质检率:"+qtRate);
		//double转换成BigDecimal型
		java.math.BigDecimal bg = new java.math.BigDecimal(qtRate);
		//进行四舍五入
		java.math.BigDecimal bgs = bg.setScale(2,BigDecimal.ROUND_HALF_UP);

		System.out.println(bg+"------------------"+bgs+"--------------------"+qualityTestingModel.getQtId());
		//把四舍五入值放入到qtRate中
		qualityTestingModel.setQtRate(bgs);


		//定义rows 运行修改方法
		int rows = qualityTestingModelDao.updateByPrimaryKeySelective(qualityTestingModel);
		//if判断rows>0时进入页面上的ok程序
		if(rows > 0){
			return "ok";
		}else {
			return "no";
		}

	}


	@Override
	public String updateTwo(QualityTestingModel qualityTestingModel) {
		qualityTestingModel.getNopassNum();
		qualityTestingModel.getQtSumnum();
		System.out.println("未通过质检数："+qualityTestingModel.getNopassNum());
		System.out.println("总质检数："+qualityTestingModel.getQtSumnum());


		int rows= qualityTestingModelDao.updateTwo(qualityTestingModel);

		//if判断rows>0时进入页面上的ok程序
		if(rows > 0){
			return "ok";
		}else {
			return "no";
		}

}



}
